package com.springclouddemo.service.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springclouddemo.common.R;
import com.springclouddemo.common.domain.User;
import com.springclouddemo.service.user.config.localization.LocalUnity;
import com.springclouddemo.service.user.mapper.UserMapper;
import com.springclouddemo.service.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户接口
 *
 * @author xiaobin
 * @date 2021/11/01 14:12
 */
@Api(tags="用户信息服务")
@RestController
@RequestMapping("api/v1/users")
public class UserController {
    private final Logger logger= LoggerFactory.getLogger("UserController");

    @Autowired
    private UserMapper userMapper;

    @GetMapping("listByOneField")
    public List<User> listByOneField(){
        System.out.println(LocalUnity.get("app.name"));
        List<User> users = userMapper.listByOneField(User::getId,1);
        //users.forEach(System.out::println);
        return users;
    }
    @GetMapping("pageByOneField")
    public Page<User> pageByOneField(){
        Page<User> userPage = userMapper.pageByOneField(new Page(1,10),User::getId,1);
        return userPage;
    }




    @Autowired
    private UserService userService;

    /*restful api*/
    @GetMapping
    @ApiOperation("查询")
    public R select(){
        return R.ok();
    }

    @GetMapping("/{dept}")
    @ApiOperation("根据部门查询")
    public R selectByDept(@PathVariable String dept, @RequestParam Map<String, Object> params){
        if(StringUtils.isBlank(dept)){
            return R.error();
        }
        return R.ok();
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @PostMapping
    @ApiOperation("新增")
    public R insert(@RequestBody User user){
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public R update(@RequestBody User user){
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除(通过Id)")
    public R delete(@PathVariable long id){
        return R.ok();
    }
    /**
     * 部分更新
     * @param id 主键
     * @param body 更新的数据
     * @return
     */
    @PatchMapping("/{id}")
    @ApiOperation("数据部分更新")
    public R patch(@PathVariable long id,@RequestBody Map<String, Object> body){
        return R.ok();
    }
    /**
     * 新增或修改
     * @param user
     * @return
     */
    @PatchMapping("/insertOrUpdate")
    @ApiOperation("新增或修改")
    public R insertOrUpdate(@RequestBody User user){
        try {
            return R.ok().put("data",user);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
    @GetMapping("/getMap")
    public Map<String, Object> getMap(@PathVariable Integer page, @PathVariable Integer limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", page);
        return map;
    }

    @RequestMapping("/findById")
    public Object findById(int userId) {
        logger.debug("findById",userId);
        User users = userService.findById(userId);
        return users;
    }

    @ApiOperation("数据查询列表(分页),动态查询条件")
    @GetMapping("/listPage")
    public R listPage(@RequestParam Map<String, Object> params){
        Long page = Long.valueOf(params.get("page").toString());
        Long limit = Long.valueOf(params.get("limit").toString());
        List list=new ArrayList<>();
        list.add(page);
        list.add(limit);
        return R.ok().put("data",list);
    }
}
